package org.jimmy.tang.lang;

import java.util.ArrayList;

/**
 * @author jimmy
 * 
 */
public class Generics {
    public static void main(String... args) {
        ArrayList<Employee5> staff = new ArrayList<Employee5>(10);
        // add方法添加元素
        staff.add(new Employee5("Bob", 4000));
        staff.add(new Employee5("Jim", 5000));
        staff.add(new Employee5("July", 8000));
        staff.add(new Employee5("Aliy", 6000));
        Employee5 e = staff.get(0);
        System.out.println(e.toString());// com.xujin.Employee5[name = Bob,
                                         // salary = 4000.0]

        // set方法将某个元素设定，该元素必须是已存在的
        staff.set(0, new Employee5("Lily", 10000));
        System.out.println(staff.get(0));// com.xujin.Employee5[name = Lily,
                                         // salary = 10000.0]
        System.out.println(staff.size());// 4

        // 把数组列表削减到当前尺寸
        staff.trimToSize();

        // remove函数实现删除一个元素
        staff.remove(0);
        System.out.println(staff.get(0));// com.xujin.Employee5[name = Jim,
                                         // salary = 5000.0]
        System.out.println(staff.size());// 3

        // add方法插入一个元素，index为3，说明3以及3之后的所有元素都后移一位
        staff.add(3, new Employee5("Ted", 6000));
        for (Employee5 em : staff) {
            System.out.println(em);
        }
        /* 
         *  com.xujin.Employee5[name = Jim, salary = 5000.0] 
            com.xujin.Employee5[name = July, salary = 8000.0] 
            com.xujin.Employee5[name = Aliy, salary = 6000.0] 
            com.xujin.Employee5[name = Ted, salary = 6000.0] 
        */
    }
}

class Employee5 {
    public Employee5(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return getClass().getName() + "[name = " + name + ", salary = "
                + salary + "]";
    }

    // 定义变量
    private double salary;
    private String name;
}
